﻿package com.kuanter.kuanterauto.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;

import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.constants.HttpConfigAPI;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.kuanter.kuanterauto.net.HttpTask;
import com.kuanter.kuanterauto.utils.DataLoader;
import com.kuanter.kuanterauto.utils.SharePrefenceUtil;
import com.kuanter.kuanterauto.utils.StringUtil;
import com.kuanter.kuanterauto.utils.StringUtils;
import com.umeng.analytics.MobclickAgent;
/**
 * 用户反馈
 * @author kuanter
 *
 */
public class UserFeedbackActivity extends WuyangBaseActivity implements
		OnClickListener {
	private EditText et_feedback;
	private TextView hasnumTV;
	private Button back_btn;
	private RelativeLayout et_feedback_bg;
	private InputMethodManager m;
	private TextView submit_tv;
	/**
	 * 请求用户反馈响应
	 *
	 */
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case HttpTask.RESULT_OK:
				finishProgress();
 				String result = (String) msg.obj;
				if (!result.equals("网络连接错误")) {
					try {
						if (StringUtil.isSuccess(result)) {// 判断返回数据中 status
							finishProgress();
							showToast("谢谢您的反馈");
						} else {
							finishProgress();
							showToast("参数错误");
						}
					} catch (JSONException e) {
						Toast.makeText(UserFeedbackActivity.this,"登录失败", 0).show();
						e.printStackTrace();
						finishProgress();
					}
				} else {
					Toast.makeText(UserFeedbackActivity.this, "登录失败,网络错误!", 0).show();
					finishProgress();
				}
				break;
			case HttpTask.RESULT_ERROR:
				Toast.makeText(UserFeedbackActivity.this, "网络错误!", 0).show();
				finishProgress();
				break;
			}
		}
	};
	private String feedbackMsg = "";
	private int userId;
	private EditText contactInfo_et;
	private String contactInfo = "";

	public void onResume() {
	    super.onResume();
	    MobclickAgent.onResume(this);
	}
	public void onPause() {
	    super.onPause();
	    MobclickAgent.onPause(this);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.user_feedback);
		super.onCreate(savedInstanceState);
		m = (InputMethodManager) this.getSystemService(INPUT_METHOD_SERVICE);
		userId = SharePrefenceUtil.getUserID(this);
		this.initBodyControlView();
		this.registerListener();
		this.setHeaderControlViewText();
	}

	@Override
	protected void setHeaderControlViewText() {
		// TODO Auto-generated method stub
		super.setHeaderTitleText(super.headerTitle, "意见反馈");
		super.setHeaderLeftText(super.headerLeft, "返回");
		super.setHeaderRightText(super.headerRight, null);

	}

	@Override
	protected void initBodyControlView() {
		// TODO Auto-generated method stub
		back_btn = (Button) this.findViewById(R.id.header_left);
		et_feedback = (EditText) this.findViewById(R.id.et_feedback);
		et_feedback_bg = (RelativeLayout) this
				.findViewById(R.id.et_feedback_bg);
		et_feedback_bg.setBackgroundResource(R.drawable.topyuanjiao);
		contactInfo_et = (EditText)this.findViewById(R.id.include_1tv_1et_et);
		hasnumTV = (TextView) findViewById(R.id.tv_feedback);
		hasnumTV.setText("0/200");
		et_feedback.addTextChangedListener(new TextWatcher() {
			private CharSequence temp;
			private int selectionStart;
			private int selectionEnd;

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				temp = s;
			}

			public void afterTextChanged(Editable s) {
				hasnumTV.setText(s.length() + "/200");
				selectionStart = et_feedback.getSelectionStart();
				selectionEnd = et_feedback.getSelectionEnd();
				if (temp.length() > 200) {
					s.delete(selectionStart - 1, selectionEnd);
					int tempSelection = selectionEnd;
					et_feedback.setText(s);
					et_feedback.setSelection(tempSelection);// 设置光标在最后
				}
			}

		});
		submit_tv = (TextView) this.findViewById(R.id.submit_tv);
	}

	@Override
	protected void setBodyControlViewText() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void registerListener() {
		// TODO Auto-generated method stub
		back_btn.setOnClickListener(this);
		et_feedback_bg.setOnClickListener(this);
		submit_tv.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.header_left:
			this.finish();
			m.hideSoftInputFromWindow(et_feedback.getWindowToken(), 0);
			break;
		case R.id.et_feedback_bg:
			// 接受软键盘输入的编辑文本或其它视图
			m.showSoftInput(et_feedback, InputMethodManager.SHOW_FORCED);
			break;

		case R.id.submit_tv:
			//showToast("提交");
			m.hideSoftInputFromWindow(et_feedback.getWindowToken(), 0);
			getControlViewText();
			if(!"".equals(feedbackMsg)&&!"".equals(contactInfo)){
				List<NameValuePair> params1 = new ArrayList<NameValuePair>();
				if(-1 != userId){
					params1.add(new BasicNameValuePair("userId", userId + ""));
				}
				params1.add(new BasicNameValuePair("feedbackMsg", feedbackMsg));
				if(StringUtils.isEmail(contactInfo)){
					params1.add(new BasicNameValuePair("contactInfo", contactInfo));
					new DataLoader(UserFeedbackActivity.this).DisplayData(MainConstants.HOST_test
							+ HttpConfigAPI.FEED_BACK, params1, handler, "", false);
					showProgress("正在加载数据...");
				}else if(isPhoneNum(contactInfo)){
					params1.add(new BasicNameValuePair("contactInfo", contactInfo));
					new DataLoader(UserFeedbackActivity.this).DisplayData(MainConstants.HOST_test
							+ HttpConfigAPI.FEED_BACK, params1, handler, "", false);
					showProgress("正在加载数据...");
				}else{
					showToast("请输入正确的联系方式");
				}
				
			}else {
				showToast("请您检查必填的内容");
			}
			
			break;
		}
	}

	// 是否是手机号
		public boolean isPhoneNum(String phoneNum) {
			String regExp = "^[1]([3][0-9]{1}|59|58|88|89|86|53|80|85|47|50|51|52|57|59|82|87|55|81|83|84|54|56|45)[0-9]{8}$";
			Pattern p = Pattern.compile(regExp);
			Matcher m = p.matcher(phoneNum);
			return m.find();// boolean
		}
	@Override
	protected void getControlViewText() {
		// TODO Auto-generated method stub
		feedbackMsg = et_feedback.getText().toString();
		contactInfo = contactInfo_et.getText().toString();
	}

}
